{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "c71eff43",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import requests\n",
    "import json\n",
    "from jbar import bar\n",
    "import time\n",
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "598fa4aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读入 C:\\Users\\EDY\\Downloads，分别读入 txList 与 itemList 两个sheet\n",
    "dfTxList = pd.read_excel(r'C:\\Users\\EDY\\Downloads\\tmp.xlsx', sheet_name='txList')\n",
    "dfItemList = pd.read_excel(r'C:\\Users\\EDY\\Downloads\\tmp.xlsx', sheet_name='itemList')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "6029a37d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_access_token():\n",
    "    \"\"\"\n",
    "    使用 API Key，Secret Key 获取access_token，替换下列示例中的应用API Key、应用Secret Key\n",
    "    \"\"\"\n",
    "    url = \"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=tV2n83G7KrSMSQuXCaLDZjya&client_secret=nHHvCwxXQGVCu8uYtjEhBuKGIznRCZng\"\n",
    "    payload = json.dumps(\"\")\n",
    "    headers = {\n",
    "        'Content-Type': 'application/json',\n",
    "        'Accept': 'application/json'\n",
    "    }\n",
    "    response = requests.request(\"POST\", url, headers=headers, data=payload)\n",
    "    return response.json().get(\"access_token\")\n",
    "def embedding(text):\n",
    "    url = \"https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/embeddings/tao_8k?access_token=\" + get_access_token()\n",
    "    payload = json.dumps({\n",
    "        \"input\": [text]\n",
    "    })\n",
    "    headers = {\n",
    "        'Content-Type': 'application/json'\n",
    "    }\n",
    "    response = requests.request(\"POST\", url, headers=headers, data=payload)\n",
    "    return response.json()['data'][0]['embedding']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "049750e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始为手术名称生成文本向量...\n",
      "100.0% - 11223/11223 - 1.8h passed finisheds left - 11/05 15:02 - 576.5ms/r                                    \n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'pickle' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[31], line 12\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[38;5;66;03m# 保存为 pickle 文件\u001b[39;00m\n\u001b[0;32m     11\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mopen\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtx_embeddings.pkl\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mwb\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m f:\n\u001b[1;32m---> 12\u001b[0m     pickle\u001b[38;5;241m.\u001b[39mdump(dataTxList, f)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'pickle' is not defined"
     ]
    }
   ],
   "source": [
    "dataTxList = dfTxList.to_dict(orient='records')\n",
    "dataItemList = dfItemList.to_dict(orient='records')\n",
    "print(\"开始为手术名称生成文本向量...\")\n",
    "for row in bar(dataTxList):\n",
    "    try:\n",
    "        row['embedding'] = embedding(row['tx_name'])\n",
    "    except:\n",
    "        time.sleep(60)\n",
    "        row['embedding'] = embedding(row['tx_name'])\n",
    "# 保存为 pickle 文件\n",
    "with open('tx_embeddings.pkl', 'wb') as f:\n",
    "    pickle.dump(dataTxList, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "fdc7d66e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始为项目名称生成文本向量...\n",
      "100.0% - 4063/4063 - 41.1m passed finishedms left - 11/05 15:52 - 607.4ms/r                                    \n"
     ]
    }
   ],
   "source": [
    "print(\"开始为项目名称生成文本向量...\")\n",
    "for row in bar(dataItemList):\n",
    "    try:\n",
    "        row['embedding'] = embedding(row['item_name'])\n",
    "    except:\n",
    "        time.sleep(60)\n",
    "        row['embedding'] = embedding(row['item_name'])\n",
    "# 保存为 pickle 文件\n",
    "with open('item_embeddings.pkl', 'wb') as f:\n",
    "    pickle.dump(dataItemList, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "506483fb",
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'tx_embeddings.pkl'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[36], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 读取 两个 pickle 文件，验证内容\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mopen\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtx_embeddings.pkl\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrb\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m f:\n\u001b[0;32m      3\u001b[0m     dataTxList \u001b[38;5;241m=\u001b[39m pickle\u001b[38;5;241m.\u001b[39mload(f)\n\u001b[0;32m      5\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mopen\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mitem_embeddings.pkl\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrb\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m f:\n",
      "File \u001b[1;32mc:\\Users\\EDY\\anaconda3\\Lib\\site-packages\\IPython\\core\\interactiveshell.py:324\u001b[0m, in \u001b[0;36m_modified_open\u001b[1;34m(file, *args, **kwargs)\u001b[0m\n\u001b[0;32m    317\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m}:\n\u001b[0;32m    318\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m    319\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIPython won\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt let you open fd=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m by default \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    320\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    321\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myou can use builtins\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m open.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    322\u001b[0m     )\n\u001b[1;32m--> 324\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m io_open(file, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'tx_embeddings.pkl'"
     ]
    }
   ],
   "source": [
    "# 读取 两个 pickle 文件，验证内容\n",
    "with open('tx_embeddings.pkl', 'rb') as f:\n",
    "    dataTxList = pickle.load(f)\n",
    "\n",
    "with open('item_embeddings.pkl', 'rb') as f:\n",
    "    dataItemList = pickle.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47b66d58",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
